package com.frkj.yuan.activity;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.frkj.yuan.BuildConfig;
import com.frkj.yuan.R;
import com.frkj.yuan.api.bean.UserLoginAutherthBean;
import com.frkj.yuan.api.category.UserLoginAutherthCategory;
import com.frkj.yuan.api.presenter.UserLoginAutherthPresenter;
import com.frkj.yuan.authLogin.BaseUIConfig;
import com.frkj.yuan.base.BaseActivity;
import com.frkj.yuan.main.ConstantStaticUrl;
import com.frkj.yuan.main.MainActivity;
import com.frkj.yuan.utils.FullScreenUtils;
import com.frkj.yuan.utils.SPUtils;
import com.frkj.yuan.utils.ToastUtil;
import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
import com.mobile.auth.gatewayauth.ResultCode;
import com.mobile.auth.gatewayauth.TokenResultListener;
import com.mobile.auth.gatewayauth.model.TokenRet;

import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;

/**
 * 信息说明: 登录认证
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/3/28 15 ：17
 * 项目名称：yuanban
 */
public class UserLoginAuthenticationActivity extends BaseActivity implements UserLoginAutherthCategory.View {
    private PhoneNumberAuthHelper mPhoneNumberAuthHelper;
    private TokenResultListener mTokenResultListener;
    private BaseUIConfig mUIConfig;
    private ProgressDialog mProgressDialog;
    private UserLoginAutherthPresenter mPresenter;
    private String isOffer;
    @Override
    public int getLayoutId() {
        return R.layout.activity_user_login_authentication;
    }
    @Override
    protected void onResume() {
        super.onResume();
        mUIConfig.onResume();
    }
    @Override
    public void initLayoutView() {
        FullScreenUtils.getInstance().fullScreen(UserLoginAuthenticationActivity.this,true);
        sdkInit(BuildConfig.AUTH_SECRET);
        mUIConfig = BaseUIConfig.init(0, UserLoginAuthenticationActivity.this, mPhoneNumberAuthHelper);
        oneKeyLogin();
//        isOffer = (String) SPUtils.get(UserLoginAuthenticationActivity.this, "offerSuccess", "");
//        if (!isOffer.equals("")) {
//            openActivityWitchAnimation(MainActivity.class);
//            finish();
//        } else {
//            mPresenter = new UserLoginAutherthPresenter(this);
//            SetPersenter(mPresenter);
//        }
    }
    private void oneKeyLogin() {
        mPhoneNumberAuthHelper = PhoneNumberAuthHelper.getInstance(UserLoginAuthenticationActivity.this, mTokenResultListener);
        mPhoneNumberAuthHelper.checkEnvAvailable();
        mUIConfig.configAuthPage();
        getLoginToken(5000);
    }
    /**
     * 拉起授权页
     *
     * @param timeout 超时时间
     */
    public void getLoginToken(int timeout) {
        mPhoneNumberAuthHelper.getLoginToken(this, timeout);
        showLoadingDialog("正在唤起授权页");
    }
    public void sdkInit(String secretInfo) {
        mTokenResultListener = new TokenResultListener() {
            @Override
            public void onTokenSuccess(String s) {
                hideLoadingDialog();
                TokenRet tokenRet = null;
                try {
                    tokenRet = TokenRet.fromJson(s);
                    if (ResultCode.CODE_START_AUTHPAGE_SUCCESS.equals(tokenRet.getCode())) {
                        Log.i("TAG", "唤起授权页成功：" + s);
                    }
                    if (ResultCode.CODE_SUCCESS.equals(tokenRet.getCode())) {
                        Log.e("secretInfo", "获取token成功：" + tokenRet.getToken());
                        mPresenter.userAuther(tokenRet.getToken());
                        mPhoneNumberAuthHelper.setAuthListener(null);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onTokenFailed(String s) {
                hideLoadingDialog();
                openActivityWitchAnimation(UserLoginActivity.class);
                finish();
            }
        };
        mPhoneNumberAuthHelper = PhoneNumberAuthHelper.getInstance(this, mTokenResultListener);
        mPhoneNumberAuthHelper.getReporter().setLoggerEnable(true);
        mPhoneNumberAuthHelper.setAuthSDKInfo(secretInfo);
    }
    public void showLoadingDialog(String hint) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        mProgressDialog.setMessage(hint);
        mProgressDialog.setCancelable(true);
        mProgressDialog.show();
    }

    public void hideLoadingDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }
    @Override
    public void userAuther(UserLoginAutherthBean loginBean) {
        if (loginBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            if (loginBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
                Log.e("loginBean",loginBean.getCode()+loginBean.getMsg());
                SPUtils.put(UserLoginAuthenticationActivity.this, ConstantStaticUrl.mToken, loginBean.getData().getToken());
                openActivityWitchAnimation(MainActivity.class);
                SPUtils.put(UserLoginAuthenticationActivity.this, "offerSuccess", "success");
                SharedPreferences sharedPreferences = getSharedPreferences("First", MODE_PRIVATE);
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putBoolean("isFirst", false);
                edit.putLong(ConstantStaticUrl.userId, loginBean.getData().getUserId());
                edit.commit();
            } else {
                ToastUtil.showToastINFO(UserLoginAuthenticationActivity.this, loginBean.getMsg());
            }
        }
    }
    @Override
    public void SetPersenter(Object persenter) {}
    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(UserLoginAuthenticationActivity.this, mErrorMsg);
    }
}
